const app = getApp()
import {getTokenByCode, tokenRegister as register} from '../../api/user'

Page({
    data: {},
    onLoad: function (options) {

    },
    toHome(){
        wx.setStorageSync('token', null);
        wx.switchTab({
            url: '/pages/index/index',
          })
    },
    toPasswordLogin(){
        wx.navigateTo({
          url: '/pages/safety/passwordLogin',
        })
    },
    getphonenumber(e) {
        if(e.detail.errMsg == "getPhoneNumber:fail user deny"){
            return false
        }
        wx.login({
            success: wxres => {
                getTokenByCode({
                    mchId: app.ISV_ID,
                    auth_code: wxres.code,
                    uuid: app.globalData.uuid,
                    pluginId: 'weixinMiniAuthPlugin'
                }).then( res => { 
                    wx.setStorageSync('token', res.data.token);
                    app.globalData.token = res.data.token
                    this.setData({
                        token:app.globalData.token
                    })
                    let params ={
                        mchId: app.ISV_ID,
                        nickname:this.data.nickname,
                        uuid: app.globalData.uuid,
                        encryptData: e.detail.encryptedData,
                        iv: e.detail.iv,
                        xuid:app.globalData.xuid
                    }
                    register(params).then( res => {
                        const {userInfo, token} = res.data
                        app.globalData.token = token
                        wx.setStorageSync('token', token);
                        app.globalData.userInfo = userInfo
                        // app.globalData.uuid = userId
                        // app.globalData.openId = userId
                        this.setData({
                            token:app.globalData.token
                        })
                        if(app.globalData.token){
                            wx.switchTab({
                              url: '/pages/my/my',
                            })
                        }
                    })
                 })
            }
        })
    },
    
});